<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <div style="margin-bottom:10px">
          <!-- TODO: 单独封装搜索组件 -->
          <emp-search
            @changeValue="changeValue"
          />
        </div>
        <!-- TODO: 单独封装树形组件 -->
        <emp-tree
          :query-params="queryParams"
          @changId="changId"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- TODO: 表格组件（不用封装, 如果封装，难度偏大，但是可以挑战，实现就好） -->
        <el-table :data="employeeList">
          <el-table-column label="头像" prop="staffPhoto" width="80" align="centter">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="username"> {{ row.username.charAt(0) }}</span>
            </template>
            <span />
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" prop="mobile" sortable />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="check(row.id)">查看</el-button>
              <el-button type="text">操作</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- TODO: 单独封装分页组件 -->
        <emp-pagination
          :query-params="queryParams"
          :total="total"
          @handleCurrentChange="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import EmpPagination from './components/emp-pagination.vue'
import empSearch from './components/emp-search.vue'
import EmpTree from './components/emp-tree.vue'
import { getEmployeeListAPI } from '@/api/employee'
export default {
  name: 'Employee',
  components: { empSearch, EmpTree, EmpPagination },
  data() {
    return {
      // 员工信息列表
      employeeList: [],
      queryParams: {
        page: 1, // 当前页码数
        pagesize: 10, // 当前页面需要的数据条数
        keyword: '', // 根据名字模糊查询
        departmentId: null // 部门id,根据部门查询当前部门及子部门的用户
      },
      total: 0 // 总条数
    }
  },
  created() {
    // 获取员工信息列表
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      // 获取员工信息列表
      const { rows, total } = await getEmployeeListAPI(this.queryParams)
      // console.log('员工信息', rows)
      // 列表信息
      this.employeeList = rows
      // 总条数
      this.total = total
    },
    // 点击切换分页
    handleCurrentChange(val) {
      this.queryParams.page = val
      // 重新获取员工信息列表
      this.getEmployeeList()
    },
    // 点击部门重新获取列表
    changId(val) {
      this.queryParams.page = val
      // 重新获取员工信息列表
      this.getEmployeeList()
    },
    // 模糊搜索
    changeValue(val) {
      this.queryParams.page = 1
      this.queryParams.keyword = val
      // 重新获取员工信息列表
      this.getEmployeeList()
    },
    // 查看
    async check(id) {
      this.$router.push(`/employee/detail/${id}`)
    }
  }

}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
